package com.yoyo.itravel.web.controller;

import com.yoyo.itravel.bean.ResultInfo;
import com.yoyo.itravel.bean.TicketTypeDetail;
import com.yoyo.itravel.exceptions.TicketTypeDetailException;
import com.yoyo.itravel.service.ITicketTypeDetailService;
import com.yoyo.itravel.utils.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description 具体门票类型服务端
 *
 * @author HZX
 * @date 2020/5/15 23:59
 */
@Controller
@RequestMapping("/ticket_detail_type")
public class TicketDetailTypeController {

    @Autowired
    private ITicketTypeDetailService ticketTypeDetailService;

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 通过具体门票类型id获得具体门票类型对象
     */
    @RequestMapping("/findTicketTypeDetailById")
    protected void findTicketTypeDetailById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得具体门票类型id
        String detailTypeId = request.getParameter("detailTypeId");

        // 构建RestInfo对象
        ResultInfo info = new ResultInfo();

        try {
            TicketTypeDetail ticketTypeDetail = ticketTypeDetailService.findByTicketDetailTypeId(detailTypeId);
            JSONUtils.writeValueToResponse(ticketTypeDetail, response);
        } catch (TicketTypeDetailException e) {
            e.printStackTrace();
            // 出现异常，将异常信息添加至info中
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        }
    }
}
